package com.acwing.partition36;

import java.io.*;

/**
 * n=2^(a1) + 2^(a2) + 2^(a3) + ... + 2^(ak)，其中 (0<=a1<a2<a3<...<ak)
 *  =2^(a1) × (1 + 2^(a2-a1) + ... + 2^(ak-a1))，等价于n中包含a1个因子2，二进制n末尾有a1个0
 *
 * n!中包含因子p的数量=n/p + n/(p^2) + n/(p^3) + ...
 * @author rkc
 * @date 2022/7/24 9:08
 */
public class AC3588排列与二进制 {

    private static int n, m;

    private static StreamTokenizer tokenizer = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
    private static PrintWriter writer = new PrintWriter(new OutputStreamWriter(System.out));

    public static void main(String[] args) throws IOException {
        while (true) {
            n = nextInt(); m = nextInt();
            if (n == 0 && m == 0) break;
            writer.println(f(n, 2) - f(n - m, 2));
        }
        writer.flush();
    }

    private static int f(int n, int p) {
        int res = 0;
        while (n != 0) {
            res += n / p;
            n /= p;
        }
        return res;
    }

    private static int nextInt() throws IOException {
        tokenizer.nextToken();
        return (int) tokenizer.nval;
    }
}
